/*
 * @Author: lihaogn
 * @Date: 2021-08-29 13:30:29
 * @LastEditTime: 2022-05-17 23:47:32
 * @LastEditor: lihaogn
 * @Description: 工具类
 * @FilePath: \lix-vue2-demos\src\utils\common.js
 */

/**
 * @author: lihaogn
 * @Date: 2022-05-17 23:45:45
 * @description: 生成 8 位随机数字
 * @return {*} String
 */
export const generateId = function() {
  return (
    Math.floor(Math.random() * 10000) + '-' + Math.floor(Math.random() * 10000)
  )
}

/**
 * @author: lihaogn
 * @Date: 2022-05-17 15:27:10
 * @description: 单位转换
 * @param {*} val - 文件大小(bytes)
 * @return {*}
 */
export const unitTranform = function(val) {
  let res = val + ' bytes'
  const aMultiples = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
  for (
    let nMultiple = 0, nApprox = val / 1024;
    nApprox > 1;
    nApprox /= 1024, nMultiple++
  ) {
    res =
      nApprox.toFixed(3) + ' ' + aMultiples[nMultiple] + ' (' + val + ' bytes)'
  }
  return res
}
